<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/1/15
 * Time: 15:24
 */
namespace app\shop\controller;
use think\Db;
class Check extends Base
{
    /**
     * 审核管理数据
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function checkPage(){
        extract(input());
        $search = input('param.search/a');
        $page = input('param.page');
        if(!$page){
            $pages = 0;
        }else{
            $pages = $page-1;
        }
        $search = $this->search($search, $page);
        //搜索条件
        $map = [];
        //任务编号/手机号
        if($search[0] != ""){
            $map['tu_number|user_phone'] = ['like',"%" . $search[0] . "%"];
        }
        //审核状态
        if($search[1] != ""){
            $map['tu_status'] = $search[1];
        }
        //审核状态
        if($search[2] != ""){
            $map['tu_task_active'] = $search[2];
        }
        //
        if(!isset($pic)){
            $pic = 1;
        }
        if(!isset($fen)){
            $fen = 1;
        }
        if($fen == 2){
            $map['task_share_d_fen'] = 2;
            if($search[3] != ""){
                $map['tu_share_fen'] = $search[3];
            }
        }else{
            $map['task_share_d_fen'] = 1;
        }
//
        $map['tu_s_id'] = session('shop_id');
        $map['tu_del'] = 1;//此用户任务未被删除
        $map['task_status'] = ['<>',5];//此任务未被删除
        if($fen == 1){
            $data = Db::name('task_user tu')
                ->join('task t','tu.tu_task_id = t.task_id','left')
                ->join('u_user u','tu.tu_user_id = u.user_id','left')
                ->where($map)
                ->field(['tu_task_active','tu_id','tu_share_fen','tu_image','tu_task_id','tu_s_id','tu_user_id','task_number','task_status','tu_number','task_type','task_price','task_name','user_username',"DATE_FORMAT(tu_receive_time, '%Y-%m-%d %H:%i')"=>'tu_receive_time',"DATE_FORMAT(tu_submit_time, '%Y-%m-%d %H:%i')"=>'tu_submit_time','tu_status','tu_reason',"DATE_FORMAT(tu_do_time, '%Y-%m-%d %H:%i')"=>'tu_do_time','task_super_first_money','task_super_admin_money'])
                ->order('tu_submit_time desc')
                ->paginate(49);
        }else{
            $data = Db::name('task_user tu')
                ->join('task t','tu.tu_task_id = t.task_id','left')
                ->join('u_user u','tu.tu_user_id = u.user_id','left')
                ->where($map)
                ->field(['tu_task_active','tu_id','tu_share_fen','tu_image','tu_task_id','tu_s_id','tu_user_id','task_number','task_status','tu_number','task_type','task_price','task_name','user_username',"DATE_FORMAT(tu_receive_time, '%Y-%m-%d %H:%i')"=>'tu_receive_time',"DATE_FORMAT(tu_submit_time, '%Y-%m-%d %H:%i')"=>'tu_submit_time','tu_status','tu_reason',"DATE_FORMAT(tu_do_time, '%Y-%m-%d %H:%i')"=>'tu_do_time','task_super_first_money','task_super_admin_money'])
                ->order('tu_submit_time desc')
                ->paginate(49)
                ->each(function($item, $key){ //分步骤的话，每步骤的钱是不一样的，不能直接取task_price
                    $newP = Db::name('task_fen')
                        ->where('tf_task_id',$item['tu_task_id'])
                        ->where('tf_order',$item['tu_share_fen'])
                        ->value('tf_money');
                    $item['task_price'] = $newP;
                    return $item;
                });
        }

        $types = ['全部', '待审核', '已通过','已拒绝','已领取','已结束','已取消','已超时'];
        $tasktypes = ['全部', '下载', '求赞求粉','问卷','投票','视频','分享'];
        $actsss = ['全部任务', '普通任务', '活动任务', '达人任务'];
        $five = ['所有', '第一步', '第二步', '第三步', '第四步', '第五步'];
        //
        $reasons = Db::name('shop_reason')
            ->where('reason_shop_id',session('shop_id'))
            ->where('reason_status',1)
            ->select();
        if($pic == 1){
            return $this->fetch ('check/check_page',['data'=>$data,'pages'=>$pages,'types'=>$types,'tasktypes'=>$tasktypes,'search'=>$search,'actsss'=>$actsss,'five'=>$five,'pic'=>$pic,'fen'=>$fen,'reasons'=>$reasons]);
        }else{
            return $this->fetch ('check/check_share_page',['data'=>$data,'pages'=>$pages,'types'=>$types,'tasktypes'=>$tasktypes,'search'=>$search,'actsss'=>$actsss,'five'=>$five,'pic'=>$pic,'fen'=>$fen,'reasons'=>$reasons]);
        }
    }

    /**
     * 任务审核详情
     * @return mixed
     */
    public function checkDetail(){
        extract(input());
        if(request ()->isPost ()){
            return $this->check($task_id,$status,$reason,$tu_id,$user_id);
        }
        $field = ['tu_task_active','tu_id','tu_task_id','tu_user_id','tu_number','task_type','task_name','user_username',"DATE_FORMAT(tu_receive_time, '%Y-%m-%d %H:%i')"=>'tu_receive_time',"DATE_FORMAT(tu_submit_time, '%Y-%m-%d %H:%i')"=>'tu_submit_time','tu_image','tu_status','tu_reason'];
        $field[] = 'task_id';
        $field[] = 'task_share_type';
        $field[] = 'task_share_num';
        $field[] = 'task_share_click';
        $field[] = 'task_share_d_fen';
        $field[] = 'task_share_b_num';
        $field[] = 'task_share_qudao';
        $field[] = 'tu_share_fen';
        $field[] = 'tu_share_b_num';
        $data = Db::name('task_user tu')
            ->join('task t','tu.tu_task_id = t.task_id','left')
            ->join('u_user u','tu.tu_user_id = u.user_id','left')
            ->where('tu_id',$tu_id)
            ->field ($field)
            ->find();
        $types = ['全部', '待审核', '已通过','已拒绝','已领取','已结束','已取消','已超时'];
        $tasktypes = ['全部', '下载', '求赞求粉','问卷','投票','视频','分享'];
        if($data['tu_image']){
            $data['tu_image'] = explode(',',$data['tu_image']);
        }
        $alls = 1;
        $son = array();
        if($data['task_share_d_fen'] == 2){
            $alls = Db::table('up_task_fen')
                ->where('tf_task_id',$data['task_id'])
                ->count();
            $son = Db::table('up_task_fen')
                ->where('tf_task_id',$data['task_id'])
                ->where('tf_order',$data['tu_share_fen'])
                ->find();
        }
        return $this->fetch ('check/check_detail',['data'=>$data,'types'=>$types,'tasktypes'=>$tasktypes,'alls'=>$alls,'son'=>$son]);
    }


    /**
     * 审核同意
     * @return \think\response\Json
     */
    public function agree(){
        extract(input());
        $info = Db::name('task_user')
            ->where('tu_id',$tu_id)
            ->field ('tu_task_id,tu_user_id,tu_status')
            ->find();
        if($info['tu_status'] != 1){
            return json(['code'=>100,'msg'=>'此任务非待审核任务，不能审核！']);
        }
        return $this->check($info['tu_task_id'],2,'',$tu_id,$info['tu_user_id']);
    }

    /**
     * 审核拒绝
     * @return \think\response\Json
     */
    public function refuse(){
        extract(input());
        $info = Db::name('task_user')
            ->where('tu_id',$tu_id)
            ->field ('tu_task_id,tu_user_id,tu_status')
            ->find();
        if($info['tu_status'] != 1){
            return json(['code'=>100,'msg'=>'此任务非待审核任务，不能审核！']);
        }
        return $this->check($info['tu_task_id'],3,$reason,$tu_id,$info['tu_user_id']);
    }


    /**
     * 批量审核
     */
    public function batchCheck(){
        extract(input());
        $havemoney = Db::name('shops')
            ->where('s_id',session('shop_id'))
            ->value('s_money');
        $tuids = trim($tuids,',');
        if(!$tuids){
            return json(['code'=>100,'msg'=>'请选择任务']);
        }
        $have = Db::name('task_user')
            ->where('tu_id','in',$tuids)
            ->where('tu_status','<>',1)
            ->find();
        if($have){
            return json(['code'=>100,'msg'=>'有非待审核任务，不能审核，请检查！']);
        }
        if($status == 2){
            if($havemoney < $money){
                return json(['code'=>100,'msg'=>'金额不足，请充值']);
            }
        }
        if($status == 3 && !$reason){
            return json(['code'=>100,'msg'=>'请输入拒绝原因']);
        }
        //OK
        return $this->check(0,$status,$reason,0,0,$tuids);
    }


    /**
     * 删除任务
     * @return \think\response\Json
     */
    public function deleteCheck(){
        $status = Db::name('task_user')->where('tu_id',input('tu_id'))->value ('tu_status');
        if($status == 1 || $status == 3){
            return json(['code'=>100,'msg'=>'待审核或已拒绝的任务不能删除']);
        }else{
            $res = Db::name('task_user')->where('tu_id',input('tu_id'))->update (['tu_del'=>2]);
            if($res){
                return json(['code'=>200,'msg'=>'删除成功']);
            }else{
                return json(['code'=>100,'msg'=>'删除失败']);
            }
        }

    }

    /**
     * 审核任务
     * @param $task_id 任务包id
     * @param $status 状态2通过 3拒绝
     * @param $reason 拒绝理由
     * @param $tu_id 任务id
     * @param $user_id 用户id
     * @return \think\response\Json
     */
    public function check($task_id,$status,$reason,$tu_id,$user_id,$tuids=''){
        $tasktype = ['error','下载','求赞求粉','问卷','投票','视频','分享'];
        Db::startTrans ();
        try{
            if($tuids){
                $push_array = array();
                $push_uids = array();
                $push_type = array();
                $idarr = explode(',',$tuids);
                foreach ($idarr as $v){
                    $info = Db::name('task_user')
                        ->where('tu_id',$v)
                        ->field ('tu_task_id,tu_user_id,tu_share_fen,tu_s_id')
                        ->find();
                    $task_id = $info['tu_task_id'];
                    $tu_id = $v;
                    $user_id = $info['tu_user_id'];
                    $shop = Db::name('task')
                        ->where('task_id',$task_id)
                        ->field(['task_status','task_price','task_type','task_active','task_share_d_fen','task_name','task_num','task_done_num','task_super_first_money','task_super_admin_money','task_lock'])
                        ->find ();
                    $push_array[] = $shop['task_name'];
                    $push_type[] = $shop['task_type'];
                    $push_uids[] = $user_id;
                    $money = Db::name('shops')
                        ->where('s_id',session('shop_id'))
                        ->value('s_money');
                    if($status == 2){//审核通过
                        if($shop['task_share_d_fen'] == 2){
                            $son = Db::table('up_task_fen')
                                ->where('tf_task_id',$task_id)
                                ->where('tf_order',$info['tu_share_fen'])
                                ->find();
                            $shop['task_price'] = $son['tf_money'];
                        }
                        if($shop['task_status'] >= 3){//任务已结束等等，需要直接从商户账户扣钱打给用户
                            if($money == 0 || $shop['task_price'] > $money){
                                return json(['code'=>100,'msg'=>'金额不足，请充值']);
                            }
                        }
                    }
                    $time = date('Y-m-d H:i:s');
                    $param = [
                        'tu_status'=>$status,
                        'tu_reason'=>$reason,
                        'tu_do_time'=>$time,
                        'tu_real_time'=>$time
                    ];
                    //如果是不在进行中的任务拒绝了，直接改为已结束
                    if($status == 3 && $shop['task_status'] >= 3){
                        $param['tu_status'] = 5;
                        if($shop['task_active'] == 3){
                            //退还商户先给的那一部分，并减掉用户的（是锁定的还是非锁定的）
                            Db::name('shops')->where('s_id',session('shop_id'))->setInc ('s_money',$shop['task_super_first_money']+$shop['task_super_admin_money']);
                            allInShopDetail($shop['task_super_first_money']+$shop['task_super_admin_money'],2,session('shop_id'),$tasktype[$shop['task_type']].'达人任务结束退还');
                            allInNotice('达人任务结束退还',$tasktype[$shop['task_type']].'达人任务结束退还，返还'.(($shop['task_super_first_money']+$shop['task_super_admin_money'])/10).'元！',3,session('shop_id'));
                            //减用户的
                            if($shop['task_lock'] == 1){
                                Db::name('u_user')->where('user_id',$user_id)->setDec('user_super_no',$shop['task_super_first_money']);
                            }else{
                               $error = Db::name('u_user')->where('user_id',$user_id)->value('user_all_money');
                                if($error-$shop['task_super_first_money'] < 0){
                                    Db::name('task')->where('task_id',$task_id)->setField('task_super_error',$shop['task_super_first_money']-$error);
                                }
                                Db::name('u_user')->where('user_id',$user_id)->setDec ('user_week_money',$shop['task_super_first_money']);
                                Db::name('u_user')->where('user_id',$user_id)->setDec ('user_month_money',$shop['task_super_first_money']);
                                Db::name('u_user')->where('user_id',$user_id)->setDec ('user_all_money',$shop['task_super_first_money']);
                            }
                            allInDetail($user_id,2,$tasktype[$shop['task_type']].'达人任务',$shop['task_super_first_money'],$shop['task_type'],$shop['task_active']);
                        }
                    }
                    Db::name('task_user')->where('tu_id',$tu_id)->update($param);
                    if($status == 2){
                        if($shop['task_active'] != 2){
                            if($shop['task_active'] == 3){
                                //给剩余的钱，给后台抽成
                                $shop['task_price'] = $shop['task_price'] - $shop['task_super_first_money'] - $shop['task_super_admin_money'];
                                allInAdminDetail(2,$shop['task_super_admin_money']/10,3,session('shop_id'));
                                //如果是先锁定的，还要解锁并处理。
                                if($shop['task_lock'] == 1){
                                    Db::name('u_user')->where('user_id',$user_id)->setInc ('user_week_money',$shop['task_super_first_money']);//审核通过，用户加钱
                                    Db::name('u_user')->where('user_id',$user_id)->setInc ('user_month_money',$shop['task_super_first_money']);//审核通过，用户加钱
                                    Db::name('u_user')->where('user_id',$user_id)->setInc ('user_all_money',$shop['task_super_first_money']);//审核通过，用户加钱

                                    Db::name('u_user')->where('user_id',$user_id)->setDec('user_super_no',$shop['task_super_first_money']);//审核通过，用户加钱
                                }
                            }
                            Db::name('u_user')->where('user_id',$user_id)->setInc ('user_week_money',$shop['task_price']);//审核通过，用户加钱
                            Db::name('u_user')->where('user_id',$user_id)->setInc ('user_month_money',$shop['task_price']);//审核通过，用户加钱
                            Db::name('u_user')->where('user_id',$user_id)->setInc ('user_all_money',$shop['task_price']);//审核通过，用户加钱
                        }else{
                            Db::name('u_user')->where('user_id',$user_id)->setInc ('user_active_money',$shop['task_price']);//审核通过，用户加钱
                        }
                        //用户入明细
                        allInDetail($user_id,1,$tasktype[$shop['task_type']].'任务',$shop['task_price'],$shop['task_type'],$shop['task_active']);
                        if($shop['task_status'] >= 3){//任务已结束，需要直接从商户账户扣钱打给用户
                            Db::name('shops')->where('s_id',session('shop_id'))->setDec ('s_money',$shop['task_price']);
                            //入商户明细和商户消息
                            allInShopDetail($shop['task_price'],1,session('shop_id'),$tasktype[$shop['task_type']].'任务扣除');
                            allInNotice('任务扣除',$tasktype[$shop['task_type']].'任务扣除，扣除'.($shop['task_price']/10).'元！',3,session('shop_id'));

                        }else{
                            if($shop['task_share_d_fen'] == 2){
                                //先加分步骤的钱数
                                Db::table('up_task')->where('task_id',$task_id)->setInc('task_share_d_fen_money',$shop['task_price']);
                                $allsteps = Db::table('up_task_fen')->where('tf_task_id',$task_id)->count();
                                if($info['tu_share_fen'] < $allsteps){
                                    $today = date('Y-m-d H:i:s');
                                    $insert = array();
                                    $insert['tu_number'] = time() . rand(10,99);
                                    $insert['tu_task_id'] = $task_id;
                                    $insert['tu_s_id'] = $info['tu_s_id'];
                                    $insert['tu_user_id'] = $user_id;
                                    $insert['tu_image'] = '';
                                    $insert['tu_receive_time'] = $today;
                                    $insert['tu_submit_time'] = '';
                                    $insert['tu_cancel_time'] = '';
                                    $insert['tu_do_time'] = '';
                                    $insert['tu_reason'] = '';
                                    $insert['tu_status'] = 4;
                                    $insert['tu_real_time'] = $today;
                                    //大富要的
                                    $insert['tu_date'] = date('Y-m-d');
                                    $insert['tu_hour'] = date('Y-m-d H');
                                    $insert['tu_type'] = $shop['task_type'];
                                    $insert['tu_task_active'] = $shop['task_active'];
                                    $insert['tu_share_fen'] = $info['tu_share_fen']+1;
                                    Db::table('up_task_user')->insert($insert);
                                }
                            }
                        }
                        if($info['tu_share_fen'] <= 1){ //不管分享分不分步骤，只要此值小于1，加就是
                            Db::name('task')->where('task_id',$task_id)->setInc ('task_done_num',1);
                            //完成提醒
                            if($shop['task_done_num']+1 >= $shop['task_num']){
                                allInNotice('任务完成提示','尊敬的商户您好，您发布的 ' . $shop['task_name'] . ' 任务已经完成！',3,session('shop_id'));
                            }
                        }
                    }
                }
            }else{
                $info = Db::name('task_user')
                    ->where('tu_id',$tu_id)
                    ->field ('tu_task_id,tu_user_id,tu_share_fen,tu_s_id')
                    ->find();
                $shop = Db::name('task')
                    ->where('task_id',$task_id)
                    ->field(['task_status','task_price','task_type','task_active','task_share_d_fen','task_name','task_num','task_done_num','task_super_first_money','task_super_admin_money','task_lock'])
                    ->find ();
                $money = Db::name('shops')
                    ->where('s_id',session('shop_id'))
                    ->value('s_money');
                if($status == 2){//审核通过
                    if($shop['task_share_d_fen'] == 2){
                        $son = Db::table('up_task_fen')
                            ->where('tf_task_id',$task_id)
                            ->where('tf_order',$info['tu_share_fen'])
                            ->find();
                        $shop['task_price'] = $son['tf_money'];
                    }
                    if($shop['task_status'] >= 3){//任务已结束等等，需要直接从商户账户扣钱打给用户
                        if($money == 0 || $shop['task_price'] > $money){
                            return json(['code'=>100,'msg'=>'金额不足，请充值']);
                        }
                    }
                }
                $time = date('Y-m-d H:i:s');
                $param = [
                    'tu_status'=>$status,
                    'tu_reason'=>$reason,
                    'tu_do_time'=>$time,
                    'tu_real_time'=>$time
                ];
                //如果是不在进行中的任务拒绝了，直接改为已结束
                if($status == 3 && $shop['task_status'] >= 3){
                    $param['tu_status'] = 5;
                    if($shop['task_active'] == 3){
                        //退还商户先给的那一部分，并减掉用户的（是锁定的还是非锁定的）
                        Db::name('shops')->where('s_id',session('shop_id'))->setInc ('s_money',$shop['task_super_first_money']+$shop['task_super_admin_money']);
                        allInShopDetail($shop['task_super_first_money']+$shop['task_super_admin_money'],2,session('shop_id'),$tasktype[$shop['task_type']].'达人任务结束退还');
                        allInNotice('达人任务结束退还',$tasktype[$shop['task_type']].'达人任务结束退还，返还'.(($shop['task_super_first_money']+$shop['task_super_admin_money'])/10).'元！',3,session('shop_id'));
                        //减用户的
                        if($shop['task_lock'] == 1){
                            Db::name('u_user')->where('user_id',$user_id)->setDec('user_super_no',$shop['task_super_first_money']);
                        }else{
                            $error = Db::name('u_user')->where('user_id',$user_id)->value('user_all_money');
                            if($error-$shop['task_super_first_money'] < 0){
                                Db::name('task')->where('task_id',$task_id)->setField('task_super_error',$shop['task_super_first_money']-$error);
                            }
                            Db::name('u_user')->where('user_id',$user_id)->setDec ('user_week_money',$shop['task_super_first_money']);
                            Db::name('u_user')->where('user_id',$user_id)->setDec ('user_month_money',$shop['task_super_first_money']);
                            Db::name('u_user')->where('user_id',$user_id)->setDec ('user_all_money',$shop['task_super_first_money']);
                        }
                        allInDetail($user_id,2,$tasktype[$shop['task_type']].'达人任务',$shop['task_super_first_money'],$shop['task_type'],$shop['task_active']);
                    }
                }
                Db::name('task_user')->where('tu_id',$tu_id)->update($param);
                if($status == 2){
                    if($shop['task_active'] != 2){
                        if($shop['task_active'] == 3){
                            //给剩余的钱，给后台抽成
                            $shop['task_price'] = $shop['task_price'] - $shop['task_super_first_money'] - $shop['task_super_admin_money'];
                            allInAdminDetail(2,$shop['task_super_admin_money']/10,3,session('shop_id'));
                            //如果是先锁定的，还要解锁并处理。
                            if($shop['task_lock'] == 1){
                                Db::name('u_user')->where('user_id',$user_id)->setInc ('user_week_money',$shop['task_super_first_money']);//审核通过，用户加钱
                                Db::name('u_user')->where('user_id',$user_id)->setInc ('user_month_money',$shop['task_super_first_money']);//审核通过，用户加钱
                                Db::name('u_user')->where('user_id',$user_id)->setInc ('user_all_money',$shop['task_super_first_money']);//审核通过，用户加钱

                                Db::name('u_user')->where('user_id',$user_id)->setDec('user_super_no',$shop['task_super_first_money']);//审核通过，用户加钱
                            }
                        }
                        Db::name('u_user')->where('user_id',$user_id)->setInc ('user_week_money',$shop['task_price']);//审核通过，用户加钱
                        Db::name('u_user')->where('user_id',$user_id)->setInc ('user_month_money',$shop['task_price']);//审核通过，用户加钱
                        Db::name('u_user')->where('user_id',$user_id)->setInc ('user_all_money',$shop['task_price']);//审核通过，用户加钱
                    }else{
                        Db::name('u_user')->where('user_id',$user_id)->setInc ('user_active_money',$shop['task_price']);//审核通过，用户加钱
                    }
                    //用户入明细
                    allInDetail($user_id,1,$tasktype[$shop['task_type']].'任务',$shop['task_price'],$shop['task_type'],$shop['task_active']);
                    if($shop['task_status'] >= 3){//任务已结束，需要直接从商户账户扣钱打给用户
                        Db::name('shops')->where('s_id',session('shop_id'))->setDec ('s_money',$shop['task_price']);
                        //入商户明细和商户消息
                        allInShopDetail($shop['task_price'],1,session('shop_id'),$tasktype[$shop['task_type']].'任务扣除');
                        allInNotice('任务扣除',$tasktype[$shop['task_type']].'任务扣除，扣除'.($shop['task_price']/10).'元！',3,session('shop_id'));
                    }else{
                        if($shop['task_share_d_fen'] == 2){
                            //先加分步骤的钱数
                            Db::table('up_task')->where('task_id',$task_id)->setInc('task_share_d_fen_money',$shop['task_price']);
                            $allsteps = Db::table('up_task_fen')->where('tf_task_id',$task_id)->count();
                            if($info['tu_share_fen'] < $allsteps){
                                $today = date('Y-m-d H:i:s');
                                $insert = array();
                                $insert['tu_number'] = time() . rand(10,99);
                                $insert['tu_task_id'] = $task_id;
                                $insert['tu_s_id'] = $info['tu_s_id'];
                                $insert['tu_user_id'] = $user_id;
                                $insert['tu_image'] = '';
                                $insert['tu_receive_time'] = $today;
                                $insert['tu_submit_time'] = '';
                                $insert['tu_cancel_time'] = '';
                                $insert['tu_do_time'] = '';
                                $insert['tu_reason'] = '';
                                $insert['tu_status'] = 4;
                                $insert['tu_real_time'] = $today;
                                //大富要的
                                $insert['tu_date'] = date('Y-m-d');
                                $insert['tu_hour'] = date('Y-m-d H');
                                $insert['tu_type'] = $shop['task_type'];
                                $insert['tu_task_active'] = $shop['task_active'];
                                $insert['tu_share_fen'] = $info['tu_share_fen']+1;
                                Db::table('up_task_user')->insert($insert);
                            }
                        }
                    }
                    if($info['tu_share_fen'] <= 1){ //不管分享分不分步骤，只要此值小于1，加就是
                        Db::name('task')->where('task_id',$task_id)->setInc ('task_done_num',1);
                        //完成提醒
                        if($shop['task_done_num']+1 >= $shop['task_num']){
                            allInNotice('任务完成提示','尊敬的商户您好，您发布的 ' . $shop['task_name'] . ' 任务已经完成！',3,session('shop_id'));
                        }
                    }
                }
            }

            Db::commit ();
            //推送给用户----------------------start
            if($tuids){
                $idarr = explode(',',$tuids);
                foreach ($idarr as $k=>$v){
                    if($status == 2){
                        $push_str = '您的 ' . $push_array[$k] . ' 任务审核通过，去查看~';
                    }else{
                        $push_str = '您的 ' . $push_array[$k] . ' 任务审核被拒，去查看~';
                    }
                    $uid = (string)$push_uids[$k];
                    try{
                        $this->Jpush(1, $uid, $push_str, array('type'=>$push_type[$k],'tu_id'=>$v,'status'=>$status));
                    }catch(\Exception $e){

                    }
                }
            }else{
                if($status == 2){
                    $push_str = '您的 ' . $shop['task_name'] . ' 任务审核通过，去查看~';
                }else{
                    $push_str = '您的 ' . $shop['task_name'] . ' 任务审核被拒，去查看~';
                }
                $uid = (string)$user_id;
                try{
                    $this->Jpush(1, $uid, $push_str, array('type'=>$shop['task_type'],'tu_id'=>$tu_id,'status'=>$status));
                }catch(\Exception $e){

                }
            }
            //推送给用户----------------------end
            return json(['code'=>200,'msg'=>'审核成功']);
        }catch (\Exception $e){
            Db::rollback ();
            return json(['code'=>100,'msg'=>'审核失败']);
        }
    }


}